Skip to content

Conversation

@Maetveis
Copy link
Contributor

The original code assumed that only special methods might be defined as defaulted. Since C++20 comparison operators might be defaulted too, and we do want to consider those as using the fields of the class.

Fixes: #116961

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-clang

Author: Mészáros Gergely (Maetveis)

Changes

The original code assumed that only special methods might be defined as defaulted. Since C++20 comparison operators might be defaulted too, and we do want to consider those as using the fields of the class.

Fixes: #116961


Full diff: https://github.com/llvm/llvm-project/pull/116965.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExprMember.cpp (+10-2)
  • (modified) clang/test/SemaCXX/warn-unused-private-field.cpp (+14)
diff --git a/clang/lib/Sema/SemaExprMember.cpp b/clang/lib/Sema/SemaExprMember.cpp
index c32df607692813..46821dc8746b2a 100644
--- a/clang/lib/Sema/SemaExprMember.cpp
+++ b/clang/lib/Sema/SemaExprMember.cpp
@@ -1874,8 +1874,16 @@ Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow,
           Context.getAttributedType(attr::NoDeref, MemberType, MemberType);
   }
 
-  auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
-  if (!(CurMethod && CurMethod->isDefaulted()))
+  auto isDefaultedSpecialMember = [this](const DeclContext *Ctx) {
+    auto *Method = dyn_cast<CXXMethodDecl>(CurContext);
+    if (!Method || !Method->isDefaulted())
+      return false;
+
+    return getDefaultedFunctionKind(Method).isSpecialMember();
+  };
+
+  // Implicit special members should not mark fields as used.
+  if (!isDefaultedSpecialMember(CurContext))
     UnusedPrivateFields.remove(Field);
 
   ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp b/clang/test/SemaCXX/warn-unused-private-field.cpp
index 1128eacc309d9f..9913dbaafb7a57 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -20,6 +20,20 @@ class SpaceShipDefaultCompare {
   int operator<=>(const SpaceShipDefaultCompare &) const = default;
 };
 
+class EqDefaultCompareOutOfClass {
+  int used;
+  bool operator==(const EqDefaultCompareOutOfClass &) const;
+};
+
+bool EqDefaultCompareOutOfClass::operator==(const EqDefaultCompareOutOfClass &) const = default;
+
+class FriendEqDefaultCompareOutOfClass {
+  int used;
+  friend bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &);
+};
+
+bool operator==(const FriendEqDefaultCompareOutOfClass &, const FriendEqDefaultCompareOutOfClass &) = default;
+
 #endif
 
 class NotFullyDefined {

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a release note, else LGTM.

…dFieldReferenceExpr

The original code assumed that only special methods might be defined
as defaulted. Since C++20 comparison operators might be defaulted too,
and we *do* want to consider those as using the fields of the class.

Fixes: llvm#116961
@Maetveis Maetveis force-pushed the unused-field-cmpr-ooc branch from af60ae1 to acffeca Compare November 25, 2024 17:40
@Maetveis Maetveis requested a review from erichkeane November 25, 2024 17:40
};

class EqDefaultCompareOutOfClass {
int used; // no warning
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain a touch better with 'no warning'? Something like, "Test to ensure that the defaulted operator results in this being seen as 'used' despite there not being code to do so."?

Copy link
Contributor Author

@Maetveis Maetveis Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sorry I wasn't sure what you were referring to, but I get now that from the change-set it is not obvious how / why the issue in #116961 is fixed, and why it is a special case not covered by the current testing. I expanded quite a bit on the comment, hopefully it did not become too verbose now.

@Maetveis Maetveis requested a review from erichkeane November 26, 2024 10:21
@Maetveis Maetveis force-pushed the unused-field-cmpr-ooc branch from af69d89 to 43cd9cd Compare November 26, 2024 10:22
@Maetveis Maetveis merged commit 9efdebc into llvm:main Nov 26, 2024
9 checks passed
@Maetveis Maetveis deleted the unused-field-cmpr-ooc branch November 26, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Clang] False positive unused private field if defaulted member comparison operator declared out-of-class

3 participants